package cn.lzwei.bilimall.coupon.service.impl;

import cn.lzwei.bilimall.coupon.entity.MemberPriceEntity;
import cn.lzwei.bilimall.coupon.entity.SkuLadderEntity;
import cn.lzwei.bilimall.coupon.service.MemberPriceService;
import cn.lzwei.bilimall.coupon.service.SkuLadderService;
import cn.lzwei.common.to.FullReductionTo;
import cn.lzwei.common.to.MemberPrice;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.lzwei.common.utils.PageUtils;
import cn.lzwei.common.utils.Query;

import cn.lzwei.bilimall.coupon.dao.SkuFullReductionDao;
import cn.lzwei.bilimall.coupon.entity.SkuFullReductionEntity;
import cn.lzwei.bilimall.coupon.service.SkuFullReductionService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Resource
    SkuLadderService skuLadderService;
    @Resource
    SkuFullReductionService skuFullReductionService;
    @Resource
    MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }
    /**
     * 批量保存：保存打折、满减、会员优惠信息
     */
    @Override
    @Transactional
    public void saveTotal(FullReductionTo fullReductionTo) {
        //保存打折、满减、会员优惠信息 （远程服务） sms_sku_ladder、sms_sku_full_reduction、sms_member_price
        //1.保存打折信息 sms_sku_ladder：    id  sku_id  full_count  discount  price   add_other
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        BeanUtils.copyProperties(fullReductionTo,skuLadderEntity);
        skuLadderEntity.setAddOther(1);
        //存在打折优惠才保存
        if (skuLadderEntity.getFullCount()>0){
            skuLadderService.save(skuLadderEntity);
        }
        //2.保存满减信息 sms_sku_full_reduction：    id  sku_id  full_price  reduce_price  add_other
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(fullReductionTo,skuFullReductionEntity);
        skuFullReductionEntity.setAddOther(1);
        //存在满减优惠才保存
        if (skuFullReductionEntity.getFullPrice().compareTo(new BigDecimal(0))==1){
            this.save(skuFullReductionEntity);
        }
        //3.保存会员优惠信息 sms_member_price：    id  sku_id  member_level_id  member_level_name  member_price  add_other
        List<MemberPrice> memberPrice = fullReductionTo.getMemberPrice();
        List<MemberPriceEntity> memberPriceEntities = memberPrice.stream().map(item -> {
            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
            memberPriceEntity.setSkuId(fullReductionTo.getSkuId());
            memberPriceEntity.setMemberLevelId(item.getId());
            memberPriceEntity.setMemberLevelName(item.getName());
            memberPriceEntity.setMemberPrice(item.getPrice());
            memberPriceEntity.setAddOther(1);
            return memberPriceEntity;
        }).filter(item->{
            //存在会员优惠才保存
            return item.getMemberPrice().compareTo(new BigDecimal(0))==1;
        }).collect(Collectors.toList());
        memberPriceService.saveBatch(memberPriceEntities);
    }

}